package com.fengchunge.service.impl;

import com.fengchunge.common.Const;
import com.fengchunge.common.ResponseCode;
import com.fengchunge.common.ServerResponse;
import com.fengchunge.dao.ServerInfoBackupMapper;
import com.fengchunge.dao.ServerInfoMapper;
import com.fengchunge.dao.SoftUpdateInformationMapper;
import com.fengchunge.pojo.ServerInfo;
import com.fengchunge.pojo.SoftUpdateInformation;
import com.fengchunge.service.IEncounterAtticService;
import com.fengchunge.util.LongIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description: service
 * @author: mingtai
 * @createDate: 22:54 2021/4/26
 * @version: 1.0
 */


@Slf4j
@Service("IEncounterAtticService")
public class EncounterAtticServiceImpl implements IEncounterAtticService {

    @Autowired
    private ServerInfoMapper serverInfoMapper;

    @Autowired
    private ServerInfoBackupMapper serverInfoBackupMapper;

    @Autowired
    private SoftUpdateInformationMapper softUpdateInformationMapper;

    private LongIdGenerator generator = new LongIdGenerator(1L);

    @Override
    public ServerResponse<String> addInfo(ServerInfo serverInfo) {

        serverInfo.setUpdateTime(new Date());
        serverInfo.setCreateTime(new Date());
        // 随机生成id
        serverInfo.setId(generator.generate());
        // 首次添加，隐藏起来   如果审批通过了，把隐藏的置为不隐藏。
        serverInfo.setDeleteStatus(Const.DeleteStatus.DELETE_STATUS_YES);
        // 首次添加，未审批,
        serverInfo.setApprovalStatus(Const.ApprovalStatus.APPROVAL_STATUS_NO);
        // 首次添加默认不逻辑删除2 逻辑删除:1-删除;2-不删除
        serverInfo.setLogicDelete(Const.LogicDelete.NO_LOGIC_DELETE);
        int insert = serverInfoMapper.insert(serverInfo);
        if (insert >= 1) {
            return ServerResponse.createBySuccessMessage("添加成功~，请去审核吧");
        }
        return ServerResponse.createByErrorMessage("添加失败，请稍后重试");
    }

    @Override
    public ServerResponse<String> editInfo(ServerInfo serverInfo) {
        log.info("修改信息入参：serverInfo = {} ", serverInfo);
        // 先看看有没有
        int resultCount = serverInfoBackupMapper.checkServerInfo(serverInfo.getId());
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("该条信息不存在~，请确认后重试~");
        }
        ServerInfo info = new ServerInfo();
        info.setUpdateTime(new Date());
        // info.setId(serverInfo.getId()) id不能被更新
        info.setNickName(serverInfo.getNickName());
        info.setWomanAge(serverInfo.getWomanAge());
        info.setServerPrice(serverInfo.getServerPrice());
        info.setAdvanceReserve(serverInfo.getAdvanceReserve());
        info.setContact(serverInfo.getContact());
        info.setUniqueServer(serverInfo.getUniqueServer());
        info.setProjectCategory(serverInfo.getProjectCategory());
        info.setAddressInfo(serverInfo.getAddressInfo());
        info.setImageAddress(serverInfo.getImageAddress());
        info.setServerCity(serverInfo.getServerCity());
        info.setCityArea(serverInfo.getCityArea());
        info.setId(serverInfo.getId());
        //  修改完的信息需要重新审核
        info.setApprovalStatus(Const.ApprovalStatus.APPROVAL_STATUS_NO);

        int updateByPrimaryKeySelective = serverInfoMapper.updateByPrimaryKeySelective(info);
        if (updateByPrimaryKeySelective <= 0) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "更新失败，请稍后重试");
        }
        return ServerResponse.createBySuccessMessage("更新信息成功，请浏览确认~");
    }


    @Override
    public ServerResponse<String> approval(Long id, Integer approvalStatus) {

        log.info("审核入参：id = {}, approvalStatus = {} ", id, approvalStatus);
        // 先看看有没有
        int resultCount = serverInfoBackupMapper.checkServerInfo(id);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("该条信息不存在~，请确认后重试~");
        }
        // 未审核的才能审核，审核通过、审核不通过不可再次审核。 审核通过的不让再次审核 审核不通过与未审核状态可以审核
        ServerInfo serverInfo = serverInfoMapper.selectByPrimaryKey(id);
        if (!serverInfo.getApprovalStatus().equals(Const.ApprovalStatus.APPROVAL_STATUS_NO)) {
            return ServerResponse.createByErrorMessage("已审核的信息不能再次审核，请确认后重试~");
        }
        // 审核通过
        int resultUpdateApprovalStatus = serverInfoBackupMapper.updateApprovalStatusById(id, approvalStatus);
        if (resultUpdateApprovalStatus > 0) {
            return ServerResponse.createBySuccessMessage("操作成功，请浏览二次确认~");
        }
        return ServerResponse.createByErrorMessage("审核失败，请稍后重试~");
    }

    @Override
    public ServerResponse<String> hidden(Long id, Integer deleteStatus) {
        log.info("修改信息入参：id = {}, deleteStatus = {} ", id, deleteStatus);
        // 先看看有没有
        int resultCount = serverInfoBackupMapper.checkServerInfo(id);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("该条信息不存在~，请确认后重试~");
        }
        // todo 逻辑判断 已经隐藏了不能再次隐藏，不隐藏的不能再次不隐藏
        // 设置隐藏状态
        int updateDeleteStatusById = serverInfoBackupMapper.updateDeleteStatusById(id, deleteStatus);
        if (updateDeleteStatusById > 0) {
            return ServerResponse.createBySuccessMessage("操作成功，请浏览二次确认~");
        }
        return ServerResponse.createByErrorMessage("操作失败，请稍后重试~");
    }

    @Override
    public ServerResponse<String> logicDelete(Long id, Integer logicDelete) {
        log.info("逻辑删除接口入参：id = {}, logicDelete= {}", id, logicDelete);
        // 先看看有没有
        int resultCount = serverInfoBackupMapper.checkServerInfo(id);
        if (resultCount == 0) {
            return ServerResponse.createByErrorMessage("该条信息不存在~，请确认后重试~");
        }
        // 设置逻辑删除
        int updateDeleteStatusById = serverInfoBackupMapper.updateLogicDeleteById(id, logicDelete);
        if (updateDeleteStatusById > 0) {
            return ServerResponse.createBySuccessMessage("操作成功，请浏览二次确认~");
        }
        return ServerResponse.createByErrorMessage("操作失败，请稍后重试~");

    }

    @Override
    public ServerResponse<ServerInfo> getDetailById(Long id) {
        ServerInfo serverInfo = serverInfoMapper.selectByPrimaryKey(id);
        if (serverInfo != null) {
            return ServerResponse.createBySuccess(serverInfo);
        }
        return ServerResponse.createByErrorMessage("没有查到此信息，请稍后重试~");
    }

    @Override
    public ServerResponse<PageInfo> manageAllList(int pageNum, int pageSize, String cityArea) {
        //startPage--start
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotBlank(cityArea)) {
            cityArea = "%" + cityArea + "%";
        }
        //填充自己的sql查询逻辑
        // 只查询没有逻辑删除的
        List<ServerInfo> serverInfoList = serverInfoBackupMapper.selectList(Const.LogicDelete.NO_LOGIC_DELETE, cityArea);
        //pageHelper-收尾
        PageInfo pageResult = new PageInfo(serverInfoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<PageInfo> generalUserList(int pageNum, int pageSize) {
        //startPage--start
        PageHelper.startPage(pageNum, pageSize);
        //填充自己的sql查询逻辑
        // 查询 不隐藏 审核通过
        List<ServerInfo> generalServerInfoList = serverInfoBackupMapper.generalUserList(Const.ApprovalStatus.APPROVAL_STATUS_YES, Const.DeleteStatus.DELETE_STATUS_NO);
        //pageHelper-收尾
        PageInfo pageResult = new PageInfo(generalServerInfoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<PageInfo> searchList(String keyword, int pageNum, int pageSize) {
        if (StringUtils.isBlank(keyword)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        if (StringUtils.isNotBlank(keyword)) {
            keyword = "%" + keyword + "%";
        }

        //startPage--start
        PageHelper.startPage(pageNum, pageSize);
        //填充自己的sql查询逻辑
        // 查询 关键字，但是不删除的
        List<ServerInfo> generalServerInfoList = serverInfoBackupMapper.searchList(Const.LogicDelete.NO_LOGIC_DELETE, keyword);
        //pageHelper-收尾
        PageInfo pageResult = new PageInfo(generalServerInfoList);
        return ServerResponse.createBySuccess(pageResult);

    }

    @Override
    public ServerResponse<PageInfo> getListByServiceTypeAndCityArea(int pageNum, int pageSize, String cityArea, Integer serviceType) {
        //startPage--start
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotBlank(cityArea)) {
            cityArea = "%" + cityArea + "%";
        }
        //填充自己的sql查询逻辑
        // 查询 关键字，但是不删除的
        List<ServerInfo> generalServerInfoList = serverInfoBackupMapper.getListByServiceTypeAndCityArea(Const.LogicDelete.NO_LOGIC_DELETE,cityArea, serviceType);
        //pageHelper-收尾
        PageInfo pageResult = new PageInfo(generalServerInfoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<SoftUpdateInformation> getUpdateInfo() {
        SoftUpdateInformation updateInfo = softUpdateInformationMapper.getUpdateInfo();
        return ServerResponse.createBySuccess(updateInfo);
    }

}



